package com.yuqid.myapplication

import android.os.Bundle
import android.util.Log
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import android.view.Menu
import android.view.MenuItem
import androidx.lifecycle.lifecycleScope
import com.bb.helper.BBHConfig
import com.bb.helper.BBHSdk
import com.bb.helper.network.Result
import com.yuqid.myapplication.databinding.ActivityMainBinding
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setSupportActionBar(binding.toolbar)

        val navController = findNavController(R.id.nav_host_fragment_content_main)
        appBarConfiguration = AppBarConfiguration(navController.graph)
        setupActionBarWithNavController(navController, appBarConfiguration)

        BBHSdk.init(
            app = application,
            config = BBHConfig(
                channel = "002",
                realChannel = "002",
                version = "9.9.9",
                appClient = "100001",
                projectId = "80"
            )
        )
        binding.fab.setOnClickListener { _ ->
            lifecycleScope.launch {

                when (BBHSdk.reportEvent(
                    hashMapOf(
                        "eventType" to "10001",
                        "payAmount" to "990000",
                    )
                )) {
                    is Result.Success -> {
                        Log.d("BBHSdk", "reportEvent success")
                    }

                    else -> {
                        Log.d("BBHSdk", "reportEvent error")
                    }
                }

                when (val result = BBHSdk.checkBlack()) {
                    is Result.Success -> {
                        Log.i("BBHSdk", "result: ${result.data}")
                    }

                    else -> {
                        if (result is Result.Error) {
                            Log.i("BBHSdk", "result error: ${result.error}")
                        } else {
                            Log.i("BBHSdk", "result error")
                        }
                    }
                }
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return when (item.itemId) {
            R.id.action_settings -> true
            else -> super.onOptionsItemSelected(item)
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        return navController.navigateUp(appBarConfiguration)
                || super.onSupportNavigateUp()
    }
}